#include <iostream>
#include <csignal>
#include <unistd.h>

/**
 * C++ raise() 函数
 *
 * raise() 函数生成信号，该函数带有一个整数信号编号作为参数
 *
 * int raise (signal sig);
 */

void signalHandler(int signum){
    std::cout << "Interrupt signal ( " << signum << " ) received.\n";

    //清理并关闭
    //关闭程序
    exit(signum);
}

int main() {
    int i = 0;
    //注册信号 SIGINT 和信号处理程序
    signal(SIGINT,signalHandler);

    while (++i){
        std::cout << "Going to sleep ..... " << std::endl;
        if (i == 3){
            raise(SIGINT);
        }
        sleep(1);
    }
    return 0;
}
